<template>
<div class="ivu-card ivu-card-bordered ivu-card-dis-hover">
  <div class="ivu-card-head"><p>{{$route.meta.title}}</p></div>

  <div class="ivu-card-body">
    <Input placeholder="请输入用户编号" style="width: 20%" v-model="modelsSearch.user_code"/>
    <Button type="primary" icon="search" @click="clickSearchUserCode">搜索</Button>
  </div>

  <div class="ivu-card-body"><Tree :data="modelsItmes" :load-data="loadDataUserCode" size="small" /></div>

</div>
</template>

<script>
import user from '@/api/user/index';

export default {
  data() {
    return {
      modelsItmes: [],
      modelsSearch: {
        user_code: ''
      }
    };
  },
  methods: {
    loadDataUserCode(item, callback) {
      user.findTeamById(item.id).then(function(res) {
        let data = [];
        if(res.models.length > 0) {
          for(let k in res.models) {
            data.push({
              title: res.models[k].user_code,
              id: res.models[k].user_id,
              loading: false,
              children: []
            });
          }
        } else {
          data.push({
            title: '无下级数据'
          });
        }

        callback(data);
      });
    },
    clickSearchUserCode() {
      var _this = this;
      user.findTeamByUserCode(this.modelsSearch.user_code).then(function(res) {
        if(res) {
          let children = [];
          if(res.models.length > 0) {
            for(let k in res.models) {
              children.push({
                title: res.models[k].user_code,
                id: res.models[k].user_id,
                loading: false,
                children: []
              });
            }
          } else {
            children.push({
              title: '无下级数据'
            });
          }

          _this.modelsItmes = [{
            title: _this.modelsSearch.user_code,
            user_code: _this.modelsSearch.user_code,
            expand: true,
            loading: false,
            children: children
          }];
        } else {
          _this.modelsItmes = [{
            title: _this.modelsSearch.user_code,
            user_code: _this.modelsSearch.user_code
          }];
        }
      });
    }
  },
  mounted() {
    if(typeof(this.$route.params.user_code) !== 'undefined') {
      this.modelsSearch.user_code = this.$route.params.user_code;
      this.clickSearchUserCode();
    }
  }
}
</script>

<style>
</style>